import io from 'socket.io-client'

export default {
    install: (app, {connection, options = {
        query: {
            source: 'pc',
            userId: 1,
            token: '999'
        },
        path: '/socket',
        transports: ['websocket'],
        autoConnect: true,
        timeout: 30 * 1000
    }}) => {
        console.log('connection = ', connection)
        const url = import.meta.env.VITE_APP_WS_URL
        const socket = io(url, options)
        socket.on('connect', data => {
            console.log('data = ', data)
        })
        socket.on('success', data => {
            console.log('success = ', data)
            socket.emit('client_send = ', { content: '已经发送模板数据！请查收' })
        })
        socket.on('send_msg', data => {
            console.log('send_msg = ', data)
        })
        socket.on('disconnect', reason => {
            if (reason === 'disconnect') {
                socket.connect()
            }
        })
        socket.on('error', error => {
            console.log('error = ', error)
        })
        console.log('socket = ', socket)
        app.config.globalProperties.$socket = socket
        app.provide('socket', socket)
    }
}
